"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库
Case Name   : 创建服务对象及外表后删除数据库成功
Create At   : 2023/9/26
Owner       : @li-xin12345
Description :
    1、创建数据库
    2、创建服务对象及外表
    3、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class RowCompressTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_bug_fix_0070'
        self.tb_name = 't_bug_fix_0070'
        self.fdw_name = 'fdw_bug_fix_0070'
        self.server = 's_bug_fix_0070'

    def test_row_compress(self):
        text = '-----step1:创建数据库;expect:成功-----'
        self.log.info(text)
        cmd = f'drop database if exists {self.db_name}; ' \
              f'create database {self.db_name};'
        res = self.pri_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, res,
                      '执行失败：' + text)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res,
                      '执行失败：' + text)

        text = '-----step2:创建服务对象及外表;expect:成功-----'
        self.log.info(text)
        sql = f'''create foreign data wrapper {self.fdw_name};
        drop server if exists {self.server};
        create server {self.server} foreign data wrapper {self.fdw_name};
        drop foreign table if exists {self.tb_name}; 
        create foreign table {self.tb_name}(a int) server {self.server};
        '''
        sql_res = self.pri_sh.execut_db_sql(sql, dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn('CREATE FOREIGN DATA WRAPPER', sql_res,
                      '执行失败：' + text)
        self.assertIn(self.constant.drop_server_success, sql_res,
                      '执行失败：' + text)
        self.assertIn(self.constant.create_server_success, sql_res,
                      '执行失败：' + text)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, sql_res,
                      '执行失败：' + text)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, sql_res,
                      '执行失败：' + text)

    def tearDown(self):
        text = '-----step3:清理环境; expect:成功-----'
        self.log.info(text)
        sql = f'drop database if exists {self.db_name};'
        drop_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(drop_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res,
                      '执行失败：' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')